import requests
import pytest
from api.recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.utils import common_assert, html_util, read_json


class TestRecharge(object):

    def setup(self):
        # 创建session对象
        ses = requests.session()
        # 实例化登录模块封装的类
        self.reg_login_api = RegLoginApi(ses)
        # 实例化充值模块封装的类
        self.recharge_api = RechargeApi(ses)
    @pytest.mark.parametrize("r,exp_status_code",read_json("recharge_data","recharge_verify_code"))
    # 获取充值验证码测试用例
    def test01_get_recharge_verify_code(self,r,exp_status_code):
        # 获取充值图片验证码
        resp = self.recharge_api.get_recharge_verify_code(r)
        # 断言
        assert exp_status_code == resp.status_code

    @pytest.mark.parametrize("money,img_code,exp_status,exp_desc",read_json("recharge_data","recharge"))
    # 充值测试用例
    def test02_recharge(self,money,img_code,exp_status,exp_desc):
        # 登录
        self.reg_login_api.user_login("18743264321", "qqq123")
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code("123400")
        # 充值
        resp = self.recharge_api.recharge(money,img_code)
        # 断言
        common_assert(resp,exp_status,exp_desc)


    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        self.reg_login_api.user_login("18743264321", "qqq123")
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code("123400")
        # 充值
        resp_recharge = self.recharge_api.recharge("100000", "8888")
        data_list = html_util(resp_recharge)
        # 获取第三方充值接口的url
        url = data_list[0]
        # 获取第三方充值接口的请求体数据
        form_dict = data_list[1]
        # 获取第三方接口返回的响应结果
        resp = self.recharge_api.third_recharge(url,form_dict)
        # 断言
        assert 200 == resp.status_code
        assert "OK" in resp.text
